package com.heima.controller;

import com.alibaba.csp.sentinel.SphO;
import com.alibaba.fastjson.JSONObject;
import com.heima.pojo.User;
import com.heima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    public static final String RESOURCE_NAME_QUERY_USER_BY_ID = "queryUserById";

    @Autowired
    UserService userService;
    @GetMapping("/get/{id}")
    public String queryUserById(@PathVariable("id") String id) {
        if (SphO.entry(RESOURCE_NAME_QUERY_USER_BY_ID)) {
            try {
                //被保护的逻辑
                //模拟数据库查询数据
                return JSONObject.toJSONString(userService.queryByUserName(id));
            } finally {
                //关闭资源
                SphO.exit();
            }
        } else {
            //资源访问阻止，被限流或被降级
            return "Resource is Block!!!";
        }
    }
}
